package com.itfsw.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.Plugin;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 实体类属性注释插件
 *
 * @author WangWei
 * @create 2018-11-12 16:04
 **/
public class ModelCommentPlugin extends PluginAdapter {
    private static final Logger logger = LoggerFactory.getLogger(ModelCommentPlugin.class);
    @Override
    public boolean validate(List<String> list) {
        // 插件使用前提是targetRuntime为MyBatis3
        if (StringUtility.stringHasValue(getContext().getTargetRuntime()) && "MyBatis3".equalsIgnoreCase(getContext().getTargetRuntime()) == false ){
            logger.warn("itfsw:插件"+this.getClass().getTypeName()+"要求运行targetRuntime必须为MyBatis3！");
            return false;
        }
        return true;
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // 添加字段注释
        StringBuffer sb = new StringBuffer();
        field.addJavaDocLine("/**");
        if (introspectedColumn.getRemarks() != null)
            field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
//        sb.append(" * 表字段 : ");
//        sb.append(introspectedTable.getFullyQualifiedTable());
//        sb.append('.');
//        sb.append(introspectedColumn.getActualColumnName());
//        field.addJavaDocLine(sb.toString());
        field.addJavaDocLine(" */");
        return true;
    }

//    @Override
//    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
//        // get方法注释
//        StringBuffer sb = new StringBuffer();
//        method.addJavaDocLine("/**");
//        method.addJavaDocLine(" * 获取：" + introspectedColumn.getRemarks());
//        method.addJavaDocLine(" *");
//        sb.append(" * @return ");
//        sb.append(introspectedColumn.getRemarks());
//        method.addJavaDocLine(sb.toString());
//        method.addJavaDocLine(" */");
//        return true;
//    }

//    @Override
//    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
//        // set方法注释
//        StringBuffer sb = new StringBuffer();
//        method.addJavaDocLine("/**");
//        method.addJavaDocLine(" * 设置：" + introspectedColumn.getRemarks());
//        method.addJavaDocLine(" *");
//        Parameter parm = method.getParameters().get(0);
//        method.addJavaDocLine(" * @param " + parm.getName());
//        sb.append(" *            ");
//        sb.append(introspectedColumn.getRemarks());
//        method.addJavaDocLine(sb.toString());
//        method.addJavaDocLine(" */");
//        return true;
//    }


    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        List<Method> methods = interfaze.getMethods();
        for (Method method : methods) {
            String method_name = method.getName();
            // 生成方法注释
            method.addJavaDocLine("/**");
            if("countByExample".equals(method_name)){
                method.addJavaDocLine(" * 根据条件获取总条数");
            } else if ("deleteByExample".equals(method_name)) {
                method.addJavaDocLine(" * 根据条件删除记录");
            } else if ("deleteByPrimaryKey".equals(method_name)) {
                method.addJavaDocLine(" * 根据主键删除记录");
            } else if ("insert".equals(method_name)) {
                method.addJavaDocLine(" * 插入全字段记录");
            } else if ("insertSelective".equals(method_name)) {
                method.addJavaDocLine(" * 插入不为空记录");
            } else if ("selectByExample".equals(method_name)) {
                method.addJavaDocLine(" * 根据条件获取记录");
            } else if ("selectByPrimaryKey".equals(method_name)) {
                method.addJavaDocLine(" * 根据主键获取一条记录");
            } else if ("updateByExampleSelective".equals(method_name)) {
                method.addJavaDocLine(" * 根据条件来更新不为空记录");
            } else if ("updateByExample".equals(method_name)) {
                method.addJavaDocLine(" * 根据条件来更新记录");
            } else if ("updateByPrimaryKey".equals(method_name)) {
                method.addJavaDocLine(" * 根据主键来更新记录");
            } else if ("selectOneByExample".equals(method_name)) {
                method.addJavaDocLine(" * 根据条件获取一条记录");
            } else if ("batchInsert".equals(method_name)) {
                method.addJavaDocLine(" * 批量插入记录");
            } else if ("batchInsertSelective".equals(method_name)) {
                method.addJavaDocLine(" * 批量插入不为空记录");
            } else if ("upsert".equals(method_name)) {
                method.addJavaDocLine(" * 不存在插入记录，存在更新记录");
            } else if ("upsertSelective".equals(method_name)) {
                method.addJavaDocLine(" * 不存在插入不为空记录，存在更新不为空记录");
            }
            method.addJavaDocLine(" *");
            List<Parameter> parameterList = method.getParameters();
            String paramterName;
            for (Parameter parameter : parameterList) {
                paramterName = parameter.getName();
                method.addJavaDocLine(" * @param " + paramterName);
            }
            method.addJavaDocLine(" * @author wangwei");
            method.addJavaDocLine(" */");
        }
        return true;
    }
}
